<?php
/**
 * Author Yulin
 * Func   彩种数据获取的方法
 */
defined('IN_YuLin') || exit('NO PERMIT!');
include_once (ROOT_PATH.'module/wap/public/public_load.inc.php');
include_once (ROOT_PATH.'module/lottery/lottery_play_conf.inc.php');
$res          = array();
// 获取当前请求的彩种
$lotteryId    = NoBadStr($_POST['lid']);
// 获取彩种信息
foreach ($lottList as $k => $v){
    if ($v['id'] == $lotteryId){
        unset($v['content']);
        $lotteryInfo = $v;
        break;
    }
}
// 投注赔率查询
$rpsTab      = Table('repoint_and_odds');
$planMainTab = Table('lottery_play_main');
$planFollTab = Table('lottery_play_follow');
$where       = '';
foreach ($rpsInfo as $k => $v){
    if ($k == $lotteryInfo['cate_id']){
        $where = ' WHERE rps.lott_cate_id = "' . $k . '" AND rps.re_point = "' . $v['re_point'] . '"';
        break;
    }
}
$sql         = 'SELECT cate.`sname`, main.`en_code` as main_encode, main.`id` as main_id, main.`title` AS main_title, tp.`title` as type_title, tp.`en_code` as type_code,
                fo.`en_code` as foll_encode, fo.`title` AS foll_title, fo.`explain`, fo.`example`, 
                rps.`re_point`, rps.`odds` 
                FROM ' . $rpsTab . ' AS rps
                LEFT JOIN ' . $planFollTab . ' AS fo
                ON rps.`follow_id` = fo.`id`
                LEFT JOIN ' . $planMainTab . ' AS main
                ON main.`id` = fo.`main_id`
                LEFT JOIN yulin_lottery_category AS cate
                ON main.`cate_id` = cate.`id`
                LEFT JOIN yulin_lottery_play_type AS tp
                ON fo.`type_id` = tp.`id`
                ' . $where;
$playList    = $db->getall($sql);
$codeKeyList = array();
foreach ($playList as $k => $v){
    $v['explain'] = html_entity_decode($v['explain']);
    $codeKeyList[$v['main_encode']][$v['type_code']][$v['foll_encode']] = $v;
}
// 同种类型的彩种载入
$sameLottery = $db->getall('SELECT id, title, ename, sname, url, thumb, ptid, periods, per_explain FROM ' . $lotteryTable . ' WHERE cate_id = ' . $lotteryInfo['cate_id']);
$data['ftime'] = time();
switch ($lotteryInfo['ptid']){
    case 'k3':
        // 获取当前彩种的期号与上期结果
        $data['playItem']    = k3Item();
        break;
    case 'syxw':    
        $data['playItem']    = syxwItem();
        break;
    case 'ssc':
        $data['playItem']    = sscItem();
        break;
}
$lotteryTab = Table('lottery_k3');
$lottingSql          = 'SELECT id, tid, period, starttime, stoptime, day, opentime, `code` FROM ' . $lotteryTab . ' WHERE tid = ' . $lotteryInfo['id'] . ' AND stoptime >= ' . time() . ' ORDER BY starttime ASC limit 2';
$shagnqiSql          = 'SELECT id, tid, period, starttime, stoptime, day, opentime, `code` FROM ' . $lotteryTab . ' WHERE tid = ' . $lotteryInfo['id'] . ' AND stoptime < ' . time() . ' ORDER BY starttime DESC limit 1';
$waitbet             = $db->getall($lottingSql);
$nextbet             = $waitbet[1];
$lotting             = $waitbet[0];
$shangqi             = $db->getrow($shagnqiSql);
// 获取100期的期号，从当前期号开始
$chasePreiodSql      = 'SELECT id, period, starttime, stoptime, opentime FROM ' . $lotteryTab . ' WHERE tid = ' . $lotteryInfo['id'] . ' AND stoptime >= ' . time() . ' ORDER BY starttime ASC limit 100';;
$chasePreData        = $db->getall($chasePreiodSql);
foreach ($chasePreData as $k1 => $v1){
    $chasePreData[$k1]['re_starttime']    = date('Y-m-d H:i:s', $v1['starttime']);
    $chasePreData[$k1]['re_stoptime']     = date('Y-m-d H:i:s', $v1['stoptime']);
    $chasePreData[$k1]['expect_opentime'] = date('Ymd H:i:s', $v1['stoptime'] + (60 * 8.5) );
}
$data['lotteryInfo'] = $lotteryInfo;
$data['lotting']     = $lotting;
$data['shangqi']     = $shangqi;
$data['nextbet']     = $nextbet;
$data['lottCateInfo']= $cateIdKey[$lotteryInfo['cate_id']];
$data['lotteryList'] = $sameLottery;
$data['chasePreiod'] = $chasePreData;
$data['codeKeyList'] = $codeKeyList;

$res['data'] = $data;
echo json_encode($res);


